// components/my-commodity/my-commodity.js
var common = require("../../utils/public.js") //引入public.js脚本
var api = require('../../utils/api.js') //api接口
Component({
  /**
   * 组件的属性列表
   */
  properties: {

    pro_list: {
      type: Array,
      value: []
    },
    login: {
      type: Boolean
    },
    loginCode: {
      type: Boolean
    },
    loginCode1: {
      type: Boolean
    },
    vipId: {
      type: Number
    },
    type: {
      type: Number
    }

  },

  /**
   * 组件的初始数据
   */
  data: {
    pro_list: [],
  },
  /**
   * 组件的方法列表
   */
  methods: {

    // 跳转商品详情
    todetail: common.throttleFunc(function(e) {

      if (wx.getStorageSync("type") == -1) {
        wx.showToast({
          title: '请您先进行身份选择',
          icon: 'none',
          duration: 1500
        })
        wx.navigateTo({
          url: '/pages/sf_xz/sf_xz?type=' + 1,
        })
      } else if (wx.getStorageSync("type") == 1) {
        if (wx.getStorageSync('codeNum') == 103) {
          //未注册商铺 跳转商铺注册页
          wx.showToast({
            title: '您还未注册店铺！',
            icon: 'none',
            duration: 1500
          })

          wx.navigateTo({
            url: '/pages/type/type',
          })


        } else if (wx.getStorageSync('codeNum') == 104) {
          //商铺审核中 
          wx.showToast({
            title: '您的商铺审核中！',
            icon: 'none',
            duration: 1000
          })

          wx.navigateTo({
            url: '/pages/shopBack/shopBack?type=' + 1,
          })



        } else if (wx.getStorageSync('codeNum') == 105) {
          //商铺审核未通过
          wx.showToast({
            title: '您申请的店铺暂未审核通过！',
            icon: 'none',
            duration: 1000
          })

          wx.navigateTo({
            url: '/pages/type/type',
          })


        } else if (wx.getStorageSync('codeNum') == 99) {

          //登录跳转商品详情界面
          wx.navigateTo({
            url: '/pages/detail/detail?id=' + e.currentTarget.dataset.id,
          })
        }

      } else if (wx.getStorageSync("type") == 2) {
        if (wx.getStorageSync('codeNum') != 99) {
          wx.showToast({
            title: '请您先注册一个收货地址',
            icon: 'none',
            duration: 1500
          })
          wx.navigateTo({
            url: '/pages/edit-address/edit-address',
          })
        } else {
          //登录跳转商品详情界面
          wx.navigateTo({
            url: '/pages/detail/detail?id=' + e.currentTarget.dataset.id,
          })
        }

      } else if (wx.getStorageSync('codeNum') == 99) {

        //登录跳转商品详情界面
        wx.navigateTo({
          url: '/pages/detail/detail?id=' + e.currentTarget.dataset.id,
        })
      }else {
        wx.navigateTo({
          url: '/pages/userMess/userMess',
        })
      }
    }),

    showCar: common.throttleFunc(function(event) {
      let that = this
      that.setData({
        showCar: event
      })
    }),
    //点击隐藏购物车
    btnStop: function(e) {
      let that = this
      if (wx.getStorageSync("type") == -1) {
        wx.showToast({
          title: '请您先进行身份选择',
          icon: 'none',
          duration: 1500
        })
        wx.navigateTo({
          url: '/pages/sf_xz/sf_xz?type=' + 1,
        })
      } else if (wx.getStorageSync("type") == 1) {
        if (wx.getStorageSync('codeNum') == 103) {
          //未注册商铺 跳转商铺注册页
          wx.showToast({
            title: '您还未注册店铺！',
            icon: 'none',
            duration: 1500
          })

          wx.navigateTo({
            url: '/pages/type/type',
          })


        } else if (wx.getStorageSync('codeNum') == 104) {
          //商铺审核中 
          wx.showToast({
            title: '您的商铺审核中！',
            icon: 'none',
            duration: 1000
          })

          wx.navigateTo({
            url: '/pages/shopBack/shopBack?type=' + 1,
          })



        } else if (wx.getStorageSync('codeNum') == 105) {
          //商铺审核未通过
          wx.showToast({
            title: '您申请的店铺暂未审核通过！',
            icon: 'none',
            duration: 1000
          })

          wx.navigateTo({
            url: '/pages/type/type',
          })


        } else if (wx.getStorageSync('codeNum') == 99) {

          that.addNum(e)
        }

      } else if (wx.getStorageSync("type") == 2) {
        if (wx.getStorageSync('codeNum') != 99) {
          wx.showToast({
            title: '请您先注册一个收货地址',
            icon: 'none',
            duration: 1500
          })
          wx.navigateTo({
            url: '/pages/edit-address/edit-address',
          })
        } else {
          that.addNum(e)
        }

      } else if (wx.getStorageSync('codeNum') == 99) {
        that.addNum(e)
      }else {
        wx.navigateTo({
          url: '/pages/userMess/userMess',
        })
      }
    },
    /**
     * 商品数量增加
     */
    addNum: function(e) {
      let that = this
      let item = e.currentTarget.dataset.item
      let id = item.id
      let list = this.data.pro_list
      let index = list.findIndex(item => item.id === id)
      let startNum = Number(list[index].goodsDetailsEntityList[0].startNum)
      console.log(startNum)
      let amount = Number(list[index].goodsDetailsEntityList[0].amount)
      if (amount == 0) {
        amount = startNum
      } else {
        amount = amount * 1 + 1
      }


      // 访问接口更新数据
      // TODO 获取userId/cityId
      let userInfoId = wx.getStorageSync("openId") //用户ID
      let cityId = wx.getStorageSync("cityId") //城市ID
      let goodsName = item.name //商品名
      let goodsDetailsId = item.goodsDetailsEntityList[0].id //商品id
      let price = 0
      let totalPrice = 0
      if (item.goodsDetailsEntityList[0].activitySeckillId == null) {
        if (wx.getStorageSync("vipFlag") == 1) {
          price = item.goodsDetailsEntityList[0].salesPriceVip
          totalPrice = price * amount
        } else {
          price = item.goodsDetailsEntityList[0].salesPrice
          totalPrice = price * amount
        }
      } else {
        price = item.goodsDetailsEntityList[0].seckillPrice
        totalPrice = price * amount
      }
      api.set_amount({
        cityId,
        userInfoId,
        goodsName,
        goodsDetailsId,
        amount,
        price,
        totalPrice
      }).then(res => {
        if (res.result) {
          list[index].goodsDetailsEntityList[0].amount = amount
          this.setData({
            pro_list: list
          })
          that.triggerEvent('getTo', {
            list
          })
          if (res.data > 0) {
            wx.setTabBarBadge({
              index: 3,
              text: res.data //显示的内容 
            })
          } else {
            wx.hideTabBarRedDot({
              index: 3
            })
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }

      })
    },
    /**
     * 商品数量减少
     */
    subNum: function(e) {
      let that = this
      let item = e.currentTarget.dataset.item
      let id = item.id
      let list = this.data.pro_list
      let index = list.findIndex(item => item.id === id)
      let startNum = Number(list[index].goodsDetailsEntityList[0].startNum)
      let amount = Number(list[index].goodsDetailsEntityList[0].amount)
      if (amount == startNum) {
        amount = 0
      } else {
        amount = amount - 1
      }

      // 访问接口更新数据

      // TODO 获取userId/cityId
      let userInfoId = wx.getStorageSync("openId") //用户ID
      let cityId = wx.getStorageSync("cityId") //城市ID
      let goodsName = item.name //商品名
      let goodsDetailsId = item.goodsDetailsEntityList[0].id //商品id
      let price = 0
      let totalPrice = 0
      if (item.goodsDetailsEntityList[0].seckillPrice == null) {
        if (wx.getStorageSync("vipFlag") == 1) {
          price = item.goodsDetailsEntityList[0].salesPriceVip
          totalPrice = price * amount
        } else {
          price = item.goodsDetailsEntityList[0].salesPrice
          totalPrice = price * amount
        }
      } else {
        price = item.goodsDetailsEntityList[0].seckillPrice
        totalPrice = price * amount
      }
      api.set_amount({
        cityId,
        userInfoId,
        goodsName,
        goodsDetailsId,
        amount,
        price,
        totalPrice
      }).then(res => {
        if (res.result) {
          list[index].goodsDetailsEntityList[0].amount = amount
          that.setData({
            pro_list: list
          })
          that.triggerEvent('getTo', {
            list
          })
          if (res.data > 0) {
            wx.setTabBarBadge({
              index: 3,
              text: res.data //显示的内容 
            })
          } else {
            wx.hideTabBarRedDot({
              index: 3
            })
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }

      })
    },
    // 同步数据
    setpro_list(list) {
      list.forEach(item => {
        if (!item.amount) {
          item.amount = 0
        }
      })
      let idList = []
      list.filter(item => {
        if (idList.findIndex(i => i.id == item.id) != -1) {
          idList.push(item)
        }
      })

      this.setData({
        pro_list: idList
      })
    },


  },
})